package com.akuhome.resource.service;

import com.akuhome.resource.dao.AuthDao;
import com.akuhome.resource.entity.Auth;
import com.akuhome.resource.entity.Character;
import com.akuhome.resource.service.entity.CharacterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionService {
    @Autowired
    AuthDao authDao;

    @Autowired
    CharacterService characterService;

    public List<Auth> findAuthByCharacterId(Integer id){
        Character character = characterService.findCharacterById(id);
        List<Auth> allAuth = authDao.findAllAuth();
        Long characterCode = character.getAuthCode();
        List<Auth> characterAuth = new ArrayList<>();
        allAuth.forEach(element ->{
            if((element.getAuthCode() & characterCode) != 0){
                characterAuth.add(element);
            }
        });
        if(characterAuth.size() == 0){
            characterAuth.add(new Auth("ROLE_NULL",0L));
        }
        return characterAuth;
    }
}
